package com.gogo.mall.service.impl;

import com.gogo.mall.dao.QyProductDao;
import com.gogo.mall.entity.QyCate;
import com.gogo.mall.dao.QyCateDao;
import com.gogo.mall.entity.QyProduct;
import com.gogo.mall.result.ResponseCode;
import com.gogo.mall.result.ResponseData;
import com.gogo.mall.service.QyCateService;
import com.gogo.mall.service.QyProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (QyCate)表服务实现类
 *
 * @author yangjing
 * @since 2021-09-03 11:58:32
 */
@Service("qyCateService")
public class QyCateServiceImpl implements QyCateService {
    @Resource
    private QyCateDao qyCateDao;

    @Resource
    private  QyProductDao qyProductDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QyCate queryById(Integer id) {
        return this.qyCateDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<QyCate> queryAllByLimit(int offset, int limit) {
        return this.qyCateDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param qyCate 实例对象
     * @return 实例对象
     */
    @Override
    public QyCate insert(QyCate qyCate) {
        this.qyCateDao.insert(qyCate);
        return qyCate;
    }

    /**
     * 修改数据
     *
     * @param qyCate 实例对象
     * @return 实例对象
     */
    @Override
    public QyCate update(QyCate qyCate) {
        this.qyCateDao.update(qyCate);
        return this.queryById(qyCate.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.qyCateDao.deleteById(id) > 0;
    }

    @Override
    public QyCate queryByRecommend(Integer id) {
        QyCate cate =qyCateDao.queryRecommend(id);
        return cate;
    }

    @Override
    public ResponseData queryCategoryById(Integer id) {
        HashMap<String, Object> resMap = new HashMap<>();
        QyCate cate = qyCateDao.queryById(id);
        List<QyProduct> productList=qyProductDao.queryByPid(id);

        resMap.put("image",cate.getImage());
        resMap.put("product",productList);
        return new ResponseData(ResponseCode.WEB_SUCCESS,resMap);
    }
}
